package com.sql.azarias.views;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.OvershootInterpolator;
import android.view.animation.Transformation;
import android.widget.ImageView;
import com.sql.azarias.R;

/**
 * Created by Administrator on 2017/2/27.
 */

public class ScrollZoomRecyclerView extends RecyclerView{
    private ImageView mImageView;
    private int mImageViewHeight;
    private int getImageViewHeight;
    public ScrollZoomRecyclerView(Context context) {
        super(context);
    }

    public ScrollZoomRecyclerView(Context context,  AttributeSet attrs) {
        super(context, attrs);
        getImageViewHeight = context.getResources().getDimensionPixelSize(R.dimen.live_video_player_height);
    }

    public ScrollZoomRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public void setZoomImageView(ImageView mImageView){
        this.mImageView = mImageView;
    }
    // 设置缩放级别---控制图片的最大拉伸高度
    // 在界面加载完毕的时候调用
    public void setViewsBounds() {
        mImageViewHeight = mImageView.getHeight();
        if (mImageViewHeight < 0) {

            mImageViewHeight = getImageViewHeight;
        }
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
        if(deltaY<0){//    下拉过度
            //ImageView 进行放大的效果
            mImageView.getLayoutParams().height = mImageView.getHeight()-deltaY;
            //不会刷新---view.layout();
            mImageView.requestLayout();


        }else {//上拉过度
            if (mImageView.getHeight() > mImageViewHeight){
                //ImageView 进行缩小的效果
                mImageView.getLayoutParams().height = mImageView.getHeight()-deltaY;
                //不会刷新---view.layout();
                mImageView.requestLayout();
            }

        }


        return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        //只有当imageview被放大过，这里才会执行缩小哦
        View header = (View)mImageView.getParent();
        //listview会划出去的高度（负数）
        int deltaY = header.getTop();
        if(mImageView.getHeight()>mImageViewHeight){
            //imageview进行缩小的效果
            mImageView.getLayoutParams().height=mImageView.getHeight()+deltaY;
            header.layout(header.getLeft(),0,header.getRight(),header.getHeight());
            mImageView.requestLayout();

        }
        super.onScrollChanged(l, t, oldl, oldt);
    }
    public  class ResetAnimation extends Animation {
        private int extraHeight;
        private int currentHeight;
        public ResetAnimation(int targetHeight){
            currentHeight =  mImageView.getHeight();
            extraHeight   =  currentHeight-targetHeight;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            mImageView.getLayoutParams().height =(int)(currentHeight-extraHeight*interpolatedTime);
            mImageView.requestLayout();
            super.applyTransformation(interpolatedTime, t);
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int aciton=ev.getAction();
        if(aciton==MotionEvent.ACTION_UP){
            //开启动画
            ResetAnimation animation = new ResetAnimation(mImageViewHeight);
            // animation.setInterpolator(new BounceInterpolator());
            animation.setInterpolator(new OvershootInterpolator());
            // animation.setInterpolator(new AnticipateInterpolator());
            animation.setDuration(500);

            mImageView.startAnimation(animation);
        }
        return super.onTouchEvent(ev);
    }

}
